﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Services.Gateway.Runtime;
using PServiceBus.Gateway.Interfaces.DTO;

namespace PServiceBus.Services.Gateway.Extensions {
    public static partial class TopicExtension {
        public static TopicInfo ToTopicInfo(this Topic topic) {
            if (topic == null) return null;
            return new TopicInfo()
            {
                Contract = topic.ContractDict,
                Description = topic.TopicDescription,
                ID = topic.TopicID,
                Name = topic.TopicName,
                IsValid = topic.IsValid,
                NeedHeader = topic.NeedMessageHeaders,
                ExpiresIn = topic.ExpiresIn,
                Headers = topic.Headers,
                Filter = topic.TopicFilter,
                PublishDicts = topic.PublishDicts,
                CaseSensitive = topic.CaseSensitive
            };
        }

        public static Topic ToTopic(this TopicInfo topicInfo) {
            if (topicInfo == null)
                return null;
            if (!topicInfo.IsValid) 
                return Topic.New(topicInfo.Name).Valid(topicInfo.IsValid);
            var topic =
                Topic.New(topicInfo.Name)
                .Description(topicInfo.Description)
                .Filter(topicInfo.Filter)
                .NeedHeaders(topicInfo.NeedHeader)
                .Id(topicInfo.ID)
                .CaseSensitiveFilter(topicInfo.CaseSensitive);
            foreach (var param in topicInfo.Contract)
                topic.AddParameter(param.Key, param.Value);
            topic.Headers = topicInfo.Headers;
            topic.ExpiresIn = topicInfo.ExpiresIn;
            topic.PublishDicts = topicInfo.PublishDicts;
            return topic;
        }
    }
}
